<script setup>
import Edit from './components/Edit.vue'

// TODO: 列表渲染
import axios from 'axios'
import { ref, onMounted } from 'vue'
const data = ref([])
const editRef = ref(null)
const getList =  async () => {
   const res = await axios.get('/list')
   data.value = res.data
}
onMounted(() => {
  getList()
})
// TODO: 删除功能
const del = async (id) =>{
  console.log(id)
  await axios.delete(`/del/${id}`)
  getList()
}
const edit = (row) => { 
  editRef.value.open(row)
}// TODO: 编辑功能

</script>

<template>
  <div class="app">
    <el-table :data="data">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name" width="150"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button type="primary" link @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" link @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <Edit ref="editRef" @edit="getList" />
</template>

<style scoped>
.app {
  width: 980px;
  margin: 100px auto 0;
}
</style>
